/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package screen;

import framework.GameEngine;
import framework.Key;
import framework.Screen;
import framework.Tile;
import framework.Util;
import game.Block;
import game.Bomb;
import game.Bomberman;
import game.Global;
import game.Menu;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author aluno
 */
public class OptionScreen extends Screen {

    private Image back;
    List<Bomb> bombs;
    Bomberman bomberman;
    Menu menu;
    ArrayList<Block> blocks;
    List<Tile> tiles;
    List<Tile> tilesObject;
    List<Tile> tilesObjectSup;
    List<Tile> tilesColision;
    int numberOfBlocks;

    public OptionScreen() {
        loadMap(Global.level);
        numberOfBlocks = blocks.size();

//        back = Util.loadImage(Global.IMG_GAME_BACK);
        bombs = new ArrayList<Bomb>();
        bomberman = new Bomberman(350, 280);
        menu = new Menu();


    }

    private void checkCollision() {

        for (Tile tilesC : tilesColision) {
            //move o bomberman em X
            //bomberman.setX(bomberman.getX() + bomberman.getSpeedX());
            //int oldSpeedX = bomberman.getSpeedX();

            //verifica se aconteceu colisao
            if (bomberman.getSprite().collidesWith(tilesC.getSprite())) {
                System.out.println("colidiu");
                bomberman.tahColidido = true;
                bomberman.update();

                //bomberman.setSpeedX(-bomberman.getSpeedX());
                //bomberman.update();
            }
            //bomberman.tahColidido = false;
            //desfaz movimento
            //bomberman.setX(bomberman.getX() - oldSpeedX);

        }
//        if (paddle.collidesWith(ball)) {
//            ball.setSpeedY(-ball.getSpeedY());
//            ball.update();
//        }
//        for (Block block : blocks) {
//            if (block.isVisible()) {
//
//                // move a bola em X
//                ball.setX(ball.getX() + ball.getSpeedX());
//                int oldSpeedX = ball.getSpeedX();
//                // houve colisao?
//                if (block.collidesWith(ball)) {
//                    block.hit();
//                    ball.setSpeedX(-ball.getSpeedX());
//                    numberOfBlocks--;
//                }
//                // desfaz movimento
//                ball.setX(ball.getX() - oldSpeedX);
//
//                // move a bola em Y
//                ball.setY(ball.getY() + ball.getSpeedY());
//                int oldSpeedY = ball.getSpeedY();
//                // houve colisao?
//                if (block.isVisible() && block.collidesWith(ball)) {
//                    block.hit();
//                    ball.setSpeedY(-ball.getSpeedY());
//                    numberOfBlocks--;
//                }
//                // desfaz movimento
//                ball.setY(ball.getY() - oldSpeedY);
//            }
//        }
    }

    private void loadMap(int level) {
        ArrayList<String> map = new ArrayList<String>();
        ArrayList<String> map_colision = new ArrayList<String>();
        ArrayList<String> map_object = new ArrayList<String>();
        ArrayList<String> map_object_sup = new ArrayList<String>();
        String fileNameMap = "data/map" + level + ".txt";
        String fileNameColision = "data/map_colision" + level + ".txt";
        String fileNameObject = "data/map_object" + level + ".txt";
        String fileNameObjectSup = "data/map_object_sup" + level + ".txt";
        String line;
        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader(fileNameMap));
            line = reader.readLine();
            while (line != null) {
                map.add(line);
                line = reader.readLine();

                System.out.println(line);
            }
            reader = new BufferedReader(new FileReader(fileNameColision));
            line = reader.readLine();
            while (line != null) {
                map_colision.add(line);
                line = reader.readLine();
            }
            reader = new BufferedReader(new FileReader(fileNameObject));
            line = reader.readLine();
            while (line != null) {
                map_object.add(line);
                line = reader.readLine();
            }
            reader = new BufferedReader(new FileReader(fileNameObjectSup));
            line = reader.readLine();
            while (line != null) {
                map_object_sup.add(line);
                line = reader.readLine();
            }

        } catch (Exception ex) {
            System.out.println("FALHA NA LEITURA DO ARQUIVO DE MAPA: " + fileNameMap);
        }

        // mapa bloco 20x20 pixels
        // 800 / 50 = 16 blocos LARGURA
        // 600 / 50 = 12 blocos ALTURA
        blocks = new ArrayList<Block>();
        for (int lin = 0; lin < map.size(); lin++) {
            String row = map.get(lin);
            for (int col = 0; col < row.length(); col++) {
                if (row.charAt(col) == 'B') {
                    blocks.add(new Block(col * 32, lin * 32));
                }
            }
        }

        //cria o mapa do piso
        tiles = new ArrayList<Tile>();
        for (int lin = 0; lin < map.size(); lin++) {
            String row = map.get(lin);
            for (int col = 0; col < row.length(); col++) {
                if (row.charAt(col) != '.') {
                    int[] frame = new int[1];
                    frame[0] = Integer.parseInt(row.charAt(col) + "");
                    tiles.add(new Tile(col * 32, lin * 32, frame));

                }
            }
        }

        //nao funciona
//        tiles = new ArrayList<Tile>();
//        for (int lin = 0; lin <map.size(); lin++){
//            String row = map.get(lin);
//            for(int col = 0; col <row.length(); col++){
//                StringTokenizer token = new StringTokenizer(row, ",");
//                while(token.hasMoreTokens()){
//                    int qtde = token.countTokens();
//                    tiles.add(new Tile(col*32, lin*32, frame));
//                }
//
//            }
//        }

        //cria o mapa de colisao
        tilesColision = new ArrayList<Tile>();
        for (int lin = 0; lin < map_colision.size(); lin++) {
            String row = map_colision.get(lin);
            for (int col = 0; col < row.length(); col++) {
                if (row.charAt(col) != '.') {
                    int[] frame = new int[1];
                    frame[0] = Integer.parseInt(row.charAt(col) + "");
                    tilesColision.add(new Tile(col * 32, lin * 32, frame));

                }
            }
        }

        //cria o mapa de objetos
        tilesObject = new ArrayList<Tile>();
        for (int lin = 0; lin < map_object.size(); lin++) {
            String row = map_object.get(lin);
            for (int col = 0; col < row.length(); col++) {
                if (row.charAt(col) != '.') {
                    int[] frame = new int[1];
                    frame[0] = Integer.parseInt(row.charAt(col) + "");
                    tilesObject.add(new Tile(col * 32, lin * 32, frame));

                }
            }
        }

        //cria o mapa de objetos que sobrepoe o personagem
        tilesObjectSup = new ArrayList<Tile>();
        for (int lin = 0; lin < map_object_sup.size(); lin++) {
            String row = map_object_sup.get(lin);
            for (int col = 0; col < row.length(); col++) {
                if (row.charAt(col) != '.') {
                    int[] frame = new int[1];
                    frame[0] = Integer.parseInt(row.charAt(col) + "");
                    tilesObjectSup.add(new Tile(col * 32, lin * 32, frame));

                }
            }
        }

    }

    @Override
    public void update() {
        //desenha a bomba na tela
        if (Key.isDown(KeyEvent.VK_SPACE)) {
            bombs.add(new Bomb(bomberman.getX(), bomberman.getY(), 5, 5));

            for (Bomb bomb : bombs) {
                bomb.update();
            }
        }
        checkCollision();
        bomberman.update();
        menu.update();

        //coloca o jogo em fullscreen
        if (Key.isDown(KeyEvent.VK_F)) {
            //coloca o jogo em modo full screen
        }

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(back, 0, 0, null);
        for (Tile tile : tiles) {
            tile.paint(g);
        }

        for (Tile tile : tilesObject) {
            tile.paint(g);
        }
        bomberman.paint(g);
        menu.paint(g);
        for (Bomb bomb : bombs) {
            if (bomb != null) {
                bomb.paint(g);
            }
        }

        for (Tile tile : tilesObjectSup) {
            tile.paint(g);
        }
        for (Tile tile : tilesColision) {
            tile.setColision(true);
            tile.paint(g);
        }


    }
}
